<template>
    <el-container>
        <el-main class="wrapper">
            <v-breadcrumb v-bind:breadcumb="breadcumb"></v-breadcrumb>
            <section class="panel" :style="{height: mainHeight+'px'}">
                <div class="panel-body">
                    <div class="btn-group" style="margin-bottom: 15px">
                        <el-button @click="list" type="primary" icon="fa fa-refresh"
                                   size="small"> 刷新
                        </el-button>
                        <el-tooltip class="item" effect="dark" content="如果提示access_token错误，请点击该按钮" placement="top">
                            <el-button @click="retoken" type="primary" icon="fa fa-refresh"
                                       size="small"> 重置
                            </el-button>
                        </el-tooltip>

                    </div>
                    <el-table  v-loading="loading"
                               element-loading-text="努力加载中..."
                               element-loading-spinner="el-icon-loading"
                               :data="tableData" :border="true" style="width: 100%">
                        <el-table-column prop="title" label="标题" width="200px"></el-table-column>
                        <el-table-column prop="keyword_text" :show-overflow-tooltip="true" label="关键词" width="250px"></el-table-column>
                        <el-table-column prop="template_id" label="模板ID" width="400px"></el-table-column>
                        <el-table-column label="操作">
                            <template slot-scope="scope">
                                <el-button type="primary" size="mini" @click="edit(scope.row)" icon="fa fa-edit" plain>
                                    编辑</el-button>
                                <el-button type="primary" size="mini" @click="push(scope.row)" icon="fa fa-edit" plain>
                                    推送</el-button>
                                <!--<el-button icon="fa fa-trash"-->
                                           <!--@click="destory(scope.row)" type="danger" size="mini" plain> 删除-->
                                <!--</el-button>-->
                            </template>
                        </el-table-column>
                    </el-table>
                </div>
            </section>
        </el-main>
        <!-- 编辑用户，不用创建用户 -->
        <el-dialog
                :title="tmpDialogTitle"
                custom-class="bh-dialog"
                top="23vh"
                @open="resetForm('dataForm')"
                :visible.sync="tmpDialog"
                width="800px">
            <el-form :model="dataForm" :rules="rules" ref="dataForm" label-width="120px" class="demo-ruleForm">
                <el-form-item label="模板ID：" prop="template_id">
                    <el-input v-model="dataForm.template_id" disabled placeholder="请填写模板ID"></el-input>
                </el-form-item>
                <el-form-item label="跳转页：" prop="page">
                    <el-input v-model="dataForm.page" placeholder="请填写指定跳转页"></el-input>
                    <el-tag type="danger" style="margin-bottom:10px;">默认首页： pages/index/index</el-tag>
                    <el-tag type="danger" style="margin-bottom:10px;">指定详情页： pages/index/index?mod=info&id=【视频编号】</el-tag>
                </el-form-item>
                <el-alert
                        title="设置关键词"
                        :closable="false"
                        type="info">
                </el-alert>

                <el-card class="box-card">
                    <div class="text item">
                        <el-form-item
                                v-for="(item, index) in tmpDialogName"
                                :label="item"
                                :key="item.key"
                                :rules="{required: true, message: item+'不能为空', trigger: 'blur'}"
                        >
                            <el-input v-model="dataForm.keyword[0][index]"></el-input>
                        </el-form-item>
                        <el-form-item label="放大关键字">
                            <el-select v-model="dataForm.emphasis" placeholder="需要放大的关键字（无）">
                                <el-option label="无" value=""></el-option>
                                <el-option v-for="(item1, index1) in tmpDialogName" :key="index1" :label="item1" :value="index1"></el-option>
                            </el-select>
                        </el-form-item>
                    </div>
                </el-card>


            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="tmpDialog = false">取 消</el-button>
                <el-button type="primary" @click="update('dataForm')">确 定</el-button>
              </span>
        </el-dialog>
    </el-container>
</template>
<script>
    import {mapGetters} from 'vuex';
    import vBreadcrumb from '../layout/breadcrumb.vue';
    import {TmplmsgList,TmplmsgPush,TmplmsgUpdate,reToken} from '../../utils/axios';
    export default {
        components:{vBreadcrumb},
        data(){
            return {
                breadcumb:[{name: "我的模板"},],
                tableData:[],//列表数据
                tmpDialog:false,
                tmpDialogTitle:'',
                tmpDialogName:[],
                tmpDialogKey:[],
                //用户表单
                dataForm:{title:null,template_id:null,page:null,keyword:[],emphasis:null},
                rules: {
                    page: [{ required: true, message: '请指定跳转页', trigger: 'blur' }],
                },
            }
        },
        activated(){
            this.$store.dispatch('setActive', '/tmplmsg');
            this.list();
        },
        computed: {
            ...mapGetters(['active','mainHeight','loading'])
        },
        methods:{

            //获取列表
            list(){
                this.$store.dispatch('setLoading', true);
                TmplmsgList().then((response)=>{
                    if (response.code) {
                        this.$message.error(response.msg);
                    } else {
                        this.$store.dispatch('setLoading', false);
                        this.tableData = response.data;
                    }
                })
            },
            //推送
            push(info){
                this.$store.dispatch('setLoading', true);
                this.$confirm('确定要推送该模板消息吗？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    TmplmsgPush({id: info.template_id}).then((response)=>{
                        this.$store.dispatch('setLoading', false);
                        if (response.code) {
                            this.$message.error(response.msg);
                        } else {
                            this.$message.success(response.msg);
                        }
                    })
                }).catch(() => {
                    this.$store.dispatch('setLoading', false);
                });

            },
            retoken(){
                this.$store.dispatch('setLoading', true);
                this.$confirm('确定要重置token吗？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    reToken().then((response)=>{
                        this.$store.dispatch('setLoading', false);
                        if (response.code) {
                            this.$message.error(response.msg);
                        } else {
                            this.$message.success(response.msg);
                        }
                    })
                }).catch(() => {
                    this.$store.dispatch('setLoading', false);
                });
            },
            //编辑
            // create(){
            //     this.tmpDialogTitle = '新增模板消息';
            //     this.tmpDialogName = '新增模板消息';
            //     this.dataForm = {keyword_text:null,template_id:null,page:null};
            //     this.tmpDialog = true;
            // },
            //编辑
            edit(info){
                console.log(info.form);
                this.tmpDialogTitle = info.title;
                this.tmpDialogName = info.keyword_text_arr;
                this.tmpDialogKey = info.keyword;
                this.dataForm = {title:info.title,template_id:info.template_id,page:info.page,keyword:info.form,emphasis:info.emphasis};
                this.tmpDialog = true;
            },
            //更新
            update(formName){
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        TmplmsgUpdate(this.dataForm).then((response)=>{
                            if(response.code){
                                this.$message.error(response.msg);
                            }else{
                                this.tmpDialog = false;
                                this.$message({
                                    type: 'success',
                                    duration: '1000',
                                    message: response.msg,
                                    onClose:() =>{
                                        this.list();
                                    }
                                });
                            }
                        });
                    }
                });
            },
            //重置表单
            resetForm(formName) {
                if (this.$refs[formName]!==undefined) {
                    this.$refs[formName].resetFields();
                }
            },
        },
    }
</script>
